package com.zzyl.serve.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.common.utils.bean.BeanUtils;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.domain.dto.NursingPlanDto;
import com.zzyl.serve.domain.vo.NursingPlanVo;
import com.zzyl.serve.domain.vo.NursingProjectPlanVo;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 *
 * @author ruoyi
 * @date 2025-05-14
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    
    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    
    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Long id) {
        //查询护理计划
        NursingPlan plan = this.getById(id);
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(plan, vo);
        
        //查询护理计划关联的关系数据
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        
        List<NursingProjectPlanVo > nursingProjectPlanVoList = nursingProjectPlans.stream().map(x -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            BeanUtils.copyProperties(x, nursingProjectPlanVo);
            nursingProjectPlanVo.setExecuteCycle(x.getExecuteCycle().toString());//字符串类型，方便前端回显
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        vo.setProjectPlans(nursingProjectPlanVoList);
        
        return vo;
    }
    
    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }
    
    /**
     * 新增护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int insertNursingPlan(NursingPlanDto nursingPlan) {
        //拷贝对象
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlan, plan);
        System.out.println(nursingPlan);
        System.out.println(plan);
        boolean resultPlan = this.save(plan);
        
        if(!resultPlan) {
            throw new BaseException("保存护理计划失败");
        }
        
        //保存护理计划
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            boolean hasEmptyProject = projectPlans.stream()
                    .anyMatch(project -> project.getProjectId() == null);
            if (hasEmptyProject) {
                throw new BaseException("护理项目信息不完整，请重新设置");
            }
            projectPlans.forEach(x -> {
                
                x.setPlanId(plan.getId());
                x.setCreateTime(LocalDateTime.now());
            });
            return nursingProjectPlanMapper.batchInsert(projectPlans);
        }
        return 0;
    }
    
    /**
     * 修改护理计划
     *
     * @param nursingPlan 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    public int updateNursingPlan(NursingPlanDto nursingPlan) {
        //拷贝对象
        NursingPlan plan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlan, plan);
        
        List<NursingProjectPlan> projectPlans = nursingPlan.getProjectPlans();
        
        //如果护理项目列表非空，删除旧关系，批量保存新关系
        if (!CollectionUtils.isEmpty(projectPlans)) {
            nursingProjectPlanMapper.delete(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, plan.getId()));
            projectPlans.forEach(x -> {
                x.setPlanId(plan.getId());
                x.setCreateTime(LocalDateTime.now());
            });
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }
        
        //不管护理项目列表是否为空，都更新护理计划
        return this.updateById(plan) ? 1 : 0;
    }
    
    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanByIds(Long[] ids) {
        //删除关联数据
        for (Long id : ids) {
            nursingProjectPlanMapper.delete(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        }
        
        //删除护理计划数据
        return this.removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }
    
    
}